package scct.yzx.dao;

import scct.yzx.model.Contact;
import scct.yzx.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContactDao {
    public Contact login(String name,String username,String relatinship) {
        Contact contact = null;
//    获取数据库连接对象
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_contact where name=? and username=?  and relationship=?";
//    预编译查询对象
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setString(2,username);

            pstmt.setString(3, relatinship);
//        查询
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String name2 = rs.getString("name");
                String username2 = rs.getString("username");
                String relationship = rs.getString("relationship");
                contact = new Contact(id,name2, username2,relationship);
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return contact;
    }
    /**
     * @param name
     * @param username
     * @param relationship 关系
     * @return 用户对象
     */

//    添加联系人
    public boolean addContact(String name,String username,String relationship) {
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String sql = "insert into sm_contact values(null,?,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setString(2,username);
            pstmt.setString(3,relationship);
            result = pstmt.executeUpdate();
        } catch (SQLException e){
            throw new RuntimeException(e);
        }
        return result == 1;
    }
    public boolean isDupname(int id){
        int count = 0;// 查到联系人的数量
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_contact where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                count =1;
            }
//            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count>0;
    }

    public List<Contact> getAllContact() {
        Connection conn = DBUtils.getConnection();
        List<Contact> contacts = new ArrayList<>();

        String sql = "select * from sm_contact";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name =rs.getString("name");
                String username = rs.getString("username");
                String relationship = rs.getString("relationship");
                contacts.add(new Contact(id,name, username,relationship));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return contacts;
    }
    public Contact getContactId(int id){
        Contact contact = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_contact where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);

            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                String name = rs.getString("name");
                String username = rs.getString("username");

                String relationship = rs.getString("relationship");
                contact = new Contact(id,name,username,relationship);

            }
            DBUtils.closeConnection(conn);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return contact;
    }

    public boolean deleteContact(int id) {
        int num = 0;
        Connection conn = DBUtils.getConnection();
        String sqlStr = "delete from sm_contact where id = ?";
        try{
            PreparedStatement pstmt = conn.prepareStatement(sqlStr);
            pstmt.setInt(1,id);
            num = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return num > 0;
    }
    public boolean updateContact(int id,String name,String username,String relationship){
        int count = 0;
        Connection conn = DBUtils.getConnection();
        String update_sql = "update sm_contact set name=? and username=? and  relationship=? where id = ?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(update_sql);
            pstmt.setString(3,relationship);
            pstmt.setString(2,username);
            pstmt.setString(1,name);
            pstmt.setInt(4,id);
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count > 0;
    }
    public ArrayList<Contact> getContactByName(String name) {
        ArrayList<Contact> contacts = new ArrayList<>();
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_contact where name like '%" + name + "%'";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name2 = rs.getString("name");
                String username = rs.getString("username");
                String relationship = rs.getString("relationship");
                contacts.add(new Contact(id, name2,username,relationship));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return contacts;
    }


}
